#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <unordered_map>
#include "log.hpp"
using namespace std;
extern Log log;
const string dictname = "/practice/lesson-test/mysocket/mytcp/dict.txt";
const string sep = ":";

// yellow:黄色...
static bool Split(string &s, string *part1, string *part2)
{
    auto pos = s.find(sep);
    if (pos == string::npos)
        return false;
    *part1 = s.substr(0, pos);
    *part2 = s.substr(pos + 1);
    return true;
}

class Init
{
public:
    Init()
    {
        ifstream in(dictname);
        if (!in.is_open())
        {
            log(Fatal, "ifstream open %s error", dictname.c_str());
            exit(1);
        }
        string line;
        while (getline(in, line))
        {
            string part1, part2;
            Split(line, &part1, &part2);
            dict.insert({part1, part2});
        }
        in.close();
    }
    string translation(const string &key)
    {
        auto iter = dict.find(key);
        if (iter == dict.end())
            return "Unknow";
        else
            return iter->second;
    }

private:
    unordered_map<string, string> dict;
};